package com.card.db.service;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.card.activity.LoginActivity;
import com.card.db.dao.UserDao;
import com.card.db.database.UserDatabase;
import com.card.db.entity.User;
import com.card.utils.ContextUtils;
import com.card.utils.StringUtils;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class UserService {
    UserDao userDao;

    public UserService(Context context) {
        //初始化数据dao
        UserDatabase userDatabase = UserDatabase.getInstance(context);
        userDao = userDatabase.getStudentDao();
    }

    //=====================对外提供的方法=================================================
    public void queryAllUser() {
        new QueryAllUserAsync(userDao).execute();
    }


    public void initData(User... users) {
        new InitDataAsync(userDao).execute(users);
    }

    /**
     * 登录
     *
     * @param telephone
     * @param password
     * @return 返回查询对象
     */
    public User login(String telephone, String password) {
        //先判断是否存在
        AsyncTask<String, Void, User> execute = new LoginAsyncTask(userDao).execute(telephone, password);
        try {
            return execute.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 注册
     *
     * @param telephone
     * @param password
     */
    public Boolean register(String telephone, String password) {
        AsyncTask<String, Void, Boolean> execute = new RegisterAsync(userDao).execute(telephone, password);

        try {
            Boolean aBoolean = execute.get();
            return aBoolean;
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    //=====================异步提供的方法=================================================

    /**
     * 登录
     */
    static class LoginAsyncTask extends AsyncTask<String, Void, User> {
        private UserDao userDao;
        private String telephone;
        private String password;

        public LoginAsyncTask(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected User doInBackground(String... strings) {
            //获取数据
            telephone = strings[0];
            password = strings[1];
            System.out.println("string[0] = " + telephone);
            System.out.println("string[1] = " + password);
            //根据电话号码查询用户
            if (StringUtils.isEmpty(telephone, password)) {
                //先判空
                //为空就提示
//                Toast.makeText(ContextUtils.getInstance(), "密码或用户名为空", Toast.LENGTH_LONG);
                return null;
            }
            //查询
            List<User> users = userDao.queryUser(telephone);
            System.out.println(users);

            if (users.size() > 0 && users.get(0) != null) {
                //如果不为空那么就继续执行操作
                if (!users.get(0).getPassword().equals(password)) {
                    //密码错误提示
//                    Toast.makeText(ContextUtils.getInstance(),"密码错误",Toast.LENGTH_LONG);
                    return null;
                }
                //如果密码正确就返回
                System.out.println("users.get(0) = " + users.get(0));
                return users.get(0);
            }
            return null;
        }
    }

    /**
     * 初始化数据异步执行
     */
    static class InitDataAsync extends AsyncTask<User, Void, Void> {

        private UserDao userDao;

        public InitDataAsync(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(User... users) {
            userDao.insertUser(users);
            return null;
        }
    }

    /**
     * 查询所有数据
     */
    static class QueryAllUserAsync extends AsyncTask<Void, Void, Void> {
        UserDao userDao;

        public QueryAllUserAsync(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            List<User> users = userDao.queryAllUsers();
            for (User user : users) {
                Log.e("data", "doInBackground: " + user.toString());
            }
            return null;
        }
    }

    static class RegisterAsync extends AsyncTask<String, Void, Boolean> {
        UserDao userDao;

        public RegisterAsync(UserDao userDao) {
            this.userDao = userDao;
        }

        @Override
        protected Boolean doInBackground(String... strings) {
            List<User> users = userDao.queryUser(strings[0]);
            if (users.size() > 0) {
                //判断是否有这个电话号码
                return false;
            }
            userDao.insertUser(new User(strings[0], strings[1]));
            return true;
        }
    }
}
